﻿using FreeSqlDemo.Services;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Excel.Utility;
using Magicodes.IE.Demo.Dto;
using Magicodes.IE.Demo.IService;
using Magicodes.IE.Demo.Modules;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Magicodes.IE.Demo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class Import : ControllerBase
    {
        private readonly IImporterService _importer;
        public Import(IImporterService importer)
        {
            _importer = importer;
        }

        /// <summary>
        /// 导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpPost,Route("DownloadTemplate")]
      public async Task<ActionResult> DownloadTemplate()
        {
            #region 文件流下载导入模板
            //var filePath = Path.Combine(Directory.GetCurrentDirectory());
            //if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath);
            //var result = await Importer.GenerateTemplateBytes<ImportStudentDto>();
            //var fs = new MemoryStream(result);
            //return new XlsxFileResult(stream: fs, fileDownloadName: "学生表导入模板");
            #endregion
            return await _importer.DownloadTemplate();
        }

        /// <summary>
        /// 上传Student表
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost,Route("Import")]
        public async Task<string> ImportStudentInfo(IFormFile file)
        {
          return  await _importer.ImportStudentInfo(file);
        }

        [HttpPost,Route("ImportStudentAndPaymentLog")]
        public async Task<string> ImportStudentAndPaymentLog(IFormFile file)
        {
         return   await _importer.ImportStudentAndPaymentLog(file);
        }
    }
}
